package com.stox.services.data.provider.manager.impl;

import java.util.List;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.stox.services.data.provider.manager.ExchangeManager;
import com.stox.services.data.provider.repository.ExchangeRepository;
import com.stox.shared.model.Exchange;

@Component
public class ExchangeManagerImpl implements ExchangeManager, InitializingBean {

	@Autowired
	private ExchangeRepository exchangeRepository;
	
	@Override
	public void afterPropertiesSet() throws Exception {
		setup();
	}
	
	private void setup(){
		List<Exchange> exchanges = findAll();
		if(exchanges.isEmpty()){
			saveAndFlush(new Exchange("1,Bombay Stock Exchange,image/india.png"));
		}
	}
	
	@Override
	public Exchange findOne(Long id) {
		return exchangeRepository.findOne(id);
	}

	@Override
	public List<Exchange> findAll() {
		return exchangeRepository.findAll();
	}
	
	@Override
	public void saveAndFlush(Exchange exchange) {
		exchangeRepository.saveAndFlush(exchange);
	}

}
